package com.qf.usefull.dict.service.impl;

import com.qf.usefull.dict.entity.Word;
import com.qf.usefull.dict.service.MyDictionary;

/**
 * 底层基于链表结构实现字典对象
 */
public class MyDictImpl03
        extends AbstractMyDictImpl
        implements MyDictionary {

    @Override
    public void addWord(String english, String chinese) {

        Word word = new Word(english, chinese);

        if (getWord() == null) {
            setWord(word);
            setIndex(word);
        } else {
            getIndex().setNext(word);
            setIndex(word);
        }


    }

    @Override
    public String findWord(String english) {
        Word index = getWord(); // 获取当前单词
        while (true) {
            if (index == null) return null; // 指针为空,没有找到

            if (index.getEnglish().equals(english)) { // 如果匹配成功,直接返回中文单词
                return index.getChinese();
            } else { // 否则,指针继续指向链表中下一个单词,继续重复判断
                index = index.getNext();
            }
        }


    }
}
